package com.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import com.frontend.ConnectionException;
import com.frontend.NetworkInterfacer;
import com.users.User;

public class DatabasePopulator {

	public static void main(String[] args) {
		
		Logger logger = Logger.getLogger("DocOffLog");
		FileHandler fh;
		try {
			// This block configure the logger with handler and formatter
			fh = new FileHandler("logs\\DoctorsOfficeLogFile.log", true);
			logger.addHandler(fh);
			logger.setLevel(Level.ALL);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		NetworkInterfacer net = null;
		try{
		net = new NetworkInterfacer(logger);
		} catch(ConnectionException e){
			e.printStackTrace();
			System.exit(-1);
		}
		
//		makeNumUsers(5, User.ADMIN, net);
//		makeNumUsers(24, User.DOCTOR, net);
//		makeNumUsers(68, User.NURSE, net);
//		makeNumUsers(2000, User.PATIENT, net);
		deleteGeneratedUsers(net);
	}
	
	public static void makeNumUsers(int num, int type, NetworkInterfacer net){
		
		System.err.println("Check 1");
		String[] maleArr = new String[1219],femaleArr = new String[4275],lastArr = new String[1000];
		try{
			BufferedReader in = new BufferedReader(new FileReader("lists/male.txt"));
			String line = in.readLine();
			for(int i = 0; i < maleArr.length; i++){
				maleArr[i] = line;
				line = in.readLine();
				System.err.println("Check 1.1." + i);
			}

			System.err.println("Check 1.1");
		} catch(IOException e){
			System.exit(-10);
		}

		try{
			BufferedReader in = new BufferedReader(new FileReader("lists/female.txt"));
			String line = in.readLine();
			for(int i = 0; i < femaleArr.length; i++){
				femaleArr[i] = line;
				line = in.readLine();
			}

			System.err.println("Check 1.2");
		} catch(IOException e){
			System.exit(-10);
		}

		try{
			BufferedReader in = new BufferedReader(new FileReader("lists/surname.txt"));
			String line = in.readLine();
			for(int i = 0; i < lastArr.length; i++){
				lastArr[i] = line;
				line = in.readLine();
			}

			System.err.println("Check 1.3");
		} catch(IOException e){
			System.exit(-10);
		}
		

		System.err.println("Check 2");
		
		for(int i = 0; i < num; i++){
			String firstName = "";
			boolean male = Math.random()>0.5;
			if(male){
				int index = (int)(Math.random()*1219);
				firstName = maleArr[index];
			} else {
				int index = (int)(Math.random()*4275);
				firstName = femaleArr[index];
			}
			
			int index = (int)(Math.random()*1000);
			String lastName = lastArr[index];
			
			String userName = firstName.charAt(0) + lastName;
			
			firstName = stdCap(firstName);
			lastName = stdCap(lastName);
			
			String fullName = firstName + " " + lastName;
			
			String password = "gen";
			
			boolean successful = true;
			
			if(type < 0)
				type = 0;
			if(type > 3)
				type = 3;
			
			try{
				net.addUser(fullName, userName, password, type);
			} catch(ConnectionException e){
				System.err.println("Failed to add patient " + userName);
				successful = false;
			}
			
			if(successful)
				System.out.println("Successfully added " + fullName + ", Patient " + i + "/" + num);
		}
	}
	
	public static void deleteGeneratedUsers(NetworkInterfacer net){
		try{
		User[] users = net.searchNames("");
		
		for(int i = 0; i < users.length; i++){
		try{
			if(matchesGeneratedStandard(users[i].getName(), users[i].getUsername())){
				System.out.println("Removing " + users[i].getName());
				net.deleteUser(users[i].getUsername());
			}
		} catch(ConnectionException e){
			System.err.println("It broke on user " + users[i].getName());
		}
		
		}
		} catch(ConnectionException e){
			System.err.println("Failed to get user list");
		}
	}
	
	public static String stdCap(String s){
		s = s.toLowerCase();
		s = s.substring(0, 1).toUpperCase() + s.substring(1);
		return s;
	}
	
	public static boolean matchesGeneratedStandard(String name, String username){
		if(!name.contains(" "))
			return false;
		String firstName = name.split(" ")[0];
		String lastName = name.split(" ")[1];
		String testUName = (firstName.charAt(0) + lastName).toLowerCase();
		return testUName.equals(username);
	}
}
